home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Direct3D / BarGraph / BarGraph.frm next >
Text File  |  2001-10-08  |  42KB  |  1,426 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "comdlg32.ocx"
  3. Begin VB.Form GraphForm 
  4.    Caption         =   "Data Analysis Bar Graph"
  5.    ClientHeight    =   6420
  6.    ClientLeft      =   60
  7.    ClientTop       =   345
  8.    ClientWidth     =   7875
  9.    BeginProperty Font 
  10.       Name            =   "MS Sans Serif"
  11.       Size            =   13.5
  12.       Charset         =   0
  13.       Weight          =   700
  14.       Underline       =   0   'False
  15.       Italic          =   0   'False
  16.       Strikethrough   =   0   'False
  17.    EndProperty
  18.    Icon            =   "BarGraph.frx":0000
  19.    LinkTopic       =   "Form1"
  20.    ScaleHeight     =   428
  21.    ScaleMode       =   3  'Pixel
  22.    ScaleWidth      =   525
  23.    StartUpPosition =   3  'Windows Default
  24.    Begin VB.CommandButton Command1 
  25.       Caption         =   "Command1"
  26.       BeginProperty Font 
  27.          Name            =   "MS Sans Serif"
  28.          Size            =   18
  29.          Charset         =   0
  30.          Weight          =   700
  31.          Underline       =   0   'False
  32.          Italic          =   0   'False
  33.          Strikethrough   =   0   'False
  34.       EndProperty
  35.       Height          =   435
  36.       Left            =   1920
  37.       TabIndex        =   0
  38.       Top             =   5820
  39.       Visible         =   0   'False
  40.       Width           =   495
  41.    End
  42.    Begin MSComDlg.CommonDialog CommonDialog1 
  43.       Left            =   1080
  44.       Top             =   5760
  45.       _ExtentX        =   847
  46.       _ExtentY        =   847
  47.       _Version        =   393216
  48.    End
  49.    Begin VB.Timer Timer1 
  50.       Enabled         =   0   'False
  51.       Interval        =   10
  52.       Left            =   240
  53.       Top             =   5760
  54.    End
  55.    Begin VB.Menu MENU_POPUP 
  56.       Caption         =   "POPUPMENU"
  57.       Visible         =   0   'False
  58.       Begin VB.Menu MENU_EXITMENU 
  59.          Caption         =   "Exit Menu!"
  60.       End
  61.       Begin VB.Menu MENU_LOAD 
  62.          Caption         =   "Load Data From File!"
  63.       End
  64.       Begin VB.Menu MENU_RESET 
  65.          Caption         =   "Reset Orientation!"
  66.       End
  67.       Begin VB.Menu MENU_BASE 
  68.          Caption         =   "Show base plane"
  69.          Checked         =   -1  'True
  70.       End
  71.       Begin VB.Menu MENU_ROTATE 
  72.          Caption         =   "Auto Rotate"
  73.          Checked         =   -1  'True
  74.       End
  75.    End
  76. End
  77. Attribute VB_Name = "GraphForm"
  78. Attribute VB_GlobalNameSpace = False
  79. Attribute VB_Creatable = False
  80. Attribute VB_PredeclaredId = True
  81. Attribute VB_Exposed = False
  82.  
  83. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  84. '
  85. '  Copyright (C) 1999-2001 Microsoft Corporation.  All Rights Reserved.
  86. '
  87. '  File:       BarGraph.frm
  88. '  Content:    Implementation of a 3D BarGraph
  89. '
  90. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  91. Option Explicit
  92.  
  93. Dim m_maxX As Double
  94. Dim m_minX As Double
  95. Dim m_maxY As Double
  96. Dim m_minY As Double
  97. Dim m_maxZ As Double
  98. Dim m_minZ As Double
  99. Dim m_maxsize As Double
  100. Dim m_minSize As Double
  101.  
  102. Dim m_extX As Double
  103. Dim m_extY As Double
  104. Dim m_extZ As Double
  105. Dim m_extSize As Double
  106.  
  107. Dim m_scalex As Single
  108. Dim m_scaley As Single
  109. Dim m_scalez As Single
  110. Dim m_scalesize As Single
  111.  
  112. Dim m_binit As Boolean
  113. Dim m_bGraphInit As Boolean
  114. Dim m_bMinimized As Boolean
  115.  
  116. Dim m_graphroot As CD3DFrame
  117. Dim m_quad1 As CD3DFrame
  118. Dim m_quad2 As CD3DFrame
  119. Dim m_XZPlaneFrame As CD3DFrame
  120.  
  121. Dim m_bRot As Boolean
  122. Dim m_bShowBase As Boolean
  123.  
  124.  
  125. Dim m_drawtext As String
  126. Dim m_drawtextpos As RECT
  127. Dim m_drawtextEnable As Boolean
  128.  
  129.  
  130. Dim m_data As Collection
  131. Dim m_hwnd As Long
  132. Dim m_vbfont As IFont
  133. Dim m_vbfont2 As IFont
  134. Dim m_font2height  As Long
  135.  
  136. Dim m_lastX As Single
  137. Dim m_lasty As Single
  138. Dim m_bMouseDown As Boolean
  139.  
  140. Dim m_Tex As Direct3DTexture8
  141.  
  142. Dim m_meshobj As D3DXMesh
  143. Dim m_meshplane As D3DXMesh
  144. Dim m_font As D3DXFont
  145. Dim m_font2 As D3DXFont
  146.  
  147. Dim m_mediadir As String
  148.  
  149. Dim m_fElapsedTime As Single
  150.  
  151. Dim m_vVelocity  As D3DVECTOR
  152. Dim m_fYawVelocity As Single
  153. Dim m_fPitchVelocity As Single
  154.  
  155. Dim m_fYaw As Single
  156. Dim m_fPitch As Single
  157. Dim m_vPosition As D3DVECTOR
  158.  
  159. Dim m_bKey(256) As Boolean
  160. Dim m_matView As D3DMATRIX
  161. Dim m_matOrientation As D3DMATRIX
  162.  
  163.  
  164. Const kdx = 256&
  165. Const kdy = 256&
  166. Const kScale = 8
  167.  
  168.  
  169. Dim m_GraphTitle As String
  170. Dim m_RowLabels As Collection
  171. Dim m_ColLabels As Collection
  172. Dim m_cols As Long
  173. Dim m_rows As Long
  174. Dim m_barmesh() As D3DXMesh
  175. Dim m_labelmesh() As D3DXMesh
  176. Dim m_LabelTex() As Direct3DTexture8
  177. Dim m_sizex As Single
  178. Dim m_sizez As Single
  179. Dim m_ColTextures() As String
  180. Dim m_RowTextures() As String
  181.  
  182. Const D3DFVF_VERTEX = D3DFVF_XYZ Or D3DFVF_NORMAL Or D3DFVF_TEX1
  183.  
  184.  
  185. Implements DirectXEvent8
  186.  
  187. Sub DestroyDeviceObjects()
  188.  
  189.     Set m_graphroot = Nothing
  190.     Set m_quad1 = Nothing
  191.     Set m_quad2 = Nothing
  192.     Set m_XZPlaneFrame = Nothing
  193.     ReDim m_LabelTex(0)
  194.     ReDim m_barmesh(0)
  195.     ReDim m_labelmesh(0)
  196.     
  197.     
  198. End Sub
  199.  
  200. Friend Sub Init(hwnd As Long, font As IFontDisp, font2 As IFontDisp)
  201.     Dim i As Long
  202.     
  203.     'Save hwnd
  204.     m_hwnd = hwnd
  205.     
  206.     'convert IFontDisp to Ifont
  207.     Set m_vbfont = font
  208.     Set m_vbfont2 = font2
  209.     
  210.     'initialized d3d
  211.     m_binit = D3DUtil_Init(hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing)
  212.         
  213.     'exit if initialization failed
  214.     If m_binit = False Then End
  215.     
  216.     
  217.     m_bRot = True
  218.     
  219.     D3DXMatrixTranslation m_matOrientation, 0, 0, 0
  220.     
  221.     m_vPosition = vec3(0, 0, -20)
  222.  
  223.     m_sizex = 1
  224.     m_sizez = 1
  225.     
  226.     Set m_RowLabels = New Collection
  227.     Set m_ColLabels = New Collection
  228.     m_RowLabels.Add "XXX"
  229.     m_ColLabels.Add "ZZZ"
  230.  
  231.     m_bShowBase = True
  232.     
  233.     DeleteDeviceObjects
  234.     InitDeviceObjects
  235.     LoadFileAsBarGraph (m_mediadir + "bargraphdata.csv")
  236.     ComputeDataExtents
  237.     RestoreDeviceObjects
  238.     BuildGraph
  239.     
  240.     
  241.     DoEvents
  242.  
  243.     'Initialze camera matrices
  244.     g_dev.GetTransform D3DTS_VIEW, m_matView
  245.  
  246.     
  247. End Sub
  248.  
  249.  
  250. Sub RestoreDeviceObjects()
  251.  
  252.     g_lWindowWidth = Me.ScaleWidth
  253.     g_lWindowHeight = Me.ScaleHeight
  254.     D3DUtil_SetupDefaultScene
  255.     
  256.     D3DUtil_SetupCamera vec3(0, 5, -20), vec3(0, 0, 0), vec3(0, 1, 0)
  257.     
  258.     'allow the application to show both sides of all surfaces
  259.     g_dev.SetRenderState D3DRS_CULLMODE, D3DCULL_NONE
  260.     
  261.     'turn on min filtering since our text is often smaller
  262.     'than original size
  263.     g_dev.SetTextureStageState 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR
  264.     
  265.     Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  266.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  267.     
  268.     
  269.     
  270.     
  271. End Sub
  272.  
  273. Sub DeleteDeviceObjects()
  274.     Set m_font = Nothing
  275.     Set m_font2 = Nothing
  276. End Sub
  277.  
  278. Private Sub ComputeDataExtents()
  279.     Dim mind As Single
  280.     Dim maxd As Single
  281.     Dim entry As DataEntry
  282.     
  283.     mind = -9E+20
  284.     maxd = 9E+20
  285.     
  286.     m_maxX = mind:    m_maxY = mind:    m_maxZ = mind:    m_maxsize = mind
  287.     m_minX = maxd:    m_minY = maxd:    m_minZ = maxd:    m_minSize = maxd
  288.    
  289.     
  290.     'Dim entry As DataEntry
  291.     For Each entry In m_data
  292.                         
  293.         If entry.datax > m_maxX Then m_maxX = entry.datax
  294.         If entry.datay > m_maxY Then m_maxY = entry.datay
  295.         If entry.dataz > m_maxZ Then m_maxZ = entry.dataz
  296.         If entry.dataSize > m_maxsize Then m_maxsize = entry.dataSize
  297.         
  298.         If entry.datax < m_minX Then m_minX = entry.datax
  299.         If entry.datay < m_minY Then m_minY = entry.datay
  300.         If entry.dataz < m_minZ Then m_minZ = entry.dataz
  301.         If entry.dataSize < m_minSize Then m_minSize = entry.dataSize
  302.                 
  303.     Next
  304.  
  305.     m_extX = m_maxX - m_minX
  306.     m_extY = m_maxY - m_minY
  307.     m_extZ = m_maxZ - m_minZ
  308.     m_extSize = m_maxsize - m_minSize
  309.     
  310.     
  311.     m_scalex = 1
  312.     m_scaley = 1
  313.     m_scalez = 1
  314.     m_scalesize = 1
  315.     
  316.     If m_maxX > Abs(m_minX) Then
  317.         If m_maxX <> 0 Then m_scalex = kScale / m_maxX
  318.     Else
  319.         If m_minX <> 0 Then m_scalex = kScale / Abs(m_minX)
  320.     End If
  321.     
  322.     If m_maxY > Abs(m_minY) Then
  323.         If m_maxY <> 0 Then m_scaley = kScale / m_maxY
  324.     Else
  325.         If m_minY <> 0 Then m_scaley = kScale / Abs(m_minY)
  326.     End If
  327.    
  328.  
  329.     If m_maxZ > Abs(m_minZ) Then
  330.         If m_maxZ <> 0 Then m_scalez = kScale / m_maxZ
  331.     Else
  332.         If m_minZ <> 0 Then m_scalez = kScale / Abs(m_minZ)
  333.     End If
  334.     
  335.     
  336.     If m_maxsize = 0 Then m_maxsize = 1
  337.     m_scalesize = 1 * (kScale) / m_maxsize
  338.         
  339.  
  340.     
  341.     'scale graph data to fit
  342.     For Each entry In m_data
  343.                      
  344.         entry.x = (entry.datax - m_maxX / 2) * m_scalex
  345.         entry.y = (entry.datay) * m_scaley / 2
  346.         entry.z = (entry.dataz - m_maxZ / 2) * m_scalez
  347.         entry.size = entry.dataSize * m_scalesize
  348.     
  349.     Next
  350.  
  351. End Sub
  352.  
  353. Public Sub AddEntry(sName As String, x As Double, y As Double, z As Double, size As Double, color As Long, data As Variant)
  354.     On Local Error GoTo errOut
  355.     Dim entry As New DataEntry
  356.     entry.dataname = sName
  357.     entry.datax = x
  358.     entry.datay = y
  359.     entry.dataz = z
  360.     entry.dataSize = size
  361.     entry.color = color
  362.     entry.data = data
  363.     m_data.Add entry
  364.     Exit Sub
  365. errOut:
  366.     MsgBox "unable to add entry"
  367. End Sub
  368.  
  369.  
  370. Public Sub DrawGraph()
  371.     Dim entry As DataEntry
  372.     Dim hr As Long
  373.         
  374.     If m_binit = False Then Exit Sub
  375.     
  376.     'See what state the device is in.
  377.     hr = g_dev.TestCooperativeLevel
  378.     If hr = D3DERR_DEVICENOTRESET Then
  379.         g_dev.Reset g_d3dpp
  380.         RestoreDeviceObjects
  381.     ElseIf hr <> 0 Then
  382.         Exit Sub
  383.     End If
  384.     
  385.     m_graphroot.UpdateFrames
  386.              
  387.     'Clear the previous render with the backgroud color
  388.     'We clear to grey but notice that we are using a hexidecimal
  389.     'number to represent Alpha Red Green and blue
  390.     D3DUtil_ClearAll &HFF808080
  391.     
  392.     'set the ambient lighting level
  393.     g_dev.SetRenderState D3DRS_AMBIENT, &HFFC0C0C0
  394.     
  395.     
  396.     g_dev.BeginScene
  397.         
  398.  
  399.         
  400.     
  401.     'only render objects underneath the xzplane
  402.     m_quad1.Enabled = False
  403.     m_quad2.Enabled = True
  404.     m_XZPlaneFrame.Enabled = False
  405.     m_graphroot.Render g_dev
  406.  
  407.     'render the objects in front of xz plane
  408.     m_quad1.Enabled = True
  409.     m_quad2.Enabled = False
  410.     m_XZPlaneFrame.Enabled = False
  411.     m_graphroot.Render g_dev
  412.  
  413.  
  414.     'DrawLines 0
  415.  
  416.    
  417.  
  418.     'draw pop up text
  419.     If m_drawtextEnable Then
  420.         m_font.Begin
  421.         g_d3dx.DrawText m_font, &HFF000000, m_drawtext, m_drawtextpos, 0
  422.         m_font.End
  423.     End If
  424.  
  425.  
  426.     'render the xzplane with transparency
  427.     If m_bShowBase Then
  428.         m_quad1.Enabled = False
  429.         m_quad2.Enabled = False
  430.         m_XZPlaneFrame.Enabled = True
  431.         m_graphroot.Render g_dev
  432.     End If
  433.     
  434.     g_dev.EndScene
  435.     
  436.     D3DUtil_PresentAll m_hwnd
  437.  
  438. End Sub
  439.  
  440.  
  441.  
  442. Public Sub BuildGraph()
  443.     If Not m_binit Then Exit Sub
  444.     
  445.     Dim entry As DataEntry
  446.     Dim material As D3DMATERIAL8
  447.     Dim newFrame As CD3DFrame
  448.     Dim mesh As D3DXMesh
  449.     Dim frameMesh As CD3DMesh
  450.     Dim i As Long, j As Long
  451.     Dim w As Single, h As Single
  452.     Dim sv As Single, ev As Single
  453.     Dim su As Single, eu As Single
  454.     Dim d3ddm As D3DDISPLAYMODE
  455.     
  456.     If m_binit = False Then Exit Sub
  457.     
  458.     Set m_graphroot = Nothing
  459.     Set m_quad1 = Nothing
  460.     Set m_quad2 = Nothing
  461.     
  462.     'Create rotatable root object
  463.     Set m_graphroot = D3DUtil_CreateFrame(Nothing)
  464.                 
  465.     'Create XZ plane for reference
  466.     material.diffuse = LONGtoD3DCOLORVALUE(&H6FC0C0C0)
  467.     material.Ambient = material.diffuse
  468.     Set m_XZPlaneFrame = D3DUtil_CreateFrame(m_graphroot)
  469.     m_XZPlaneFrame.AddD3DXMesh(m_meshplane).SetMaterialOverride material
  470.     m_XZPlaneFrame.SetOrientation D3DUtil_RotationAxis(1, 0, 0, 90)
  471.     
  472.     Set m_quad1 = D3DUtil_CreateFrame(m_graphroot)
  473.     Set m_quad2 = D3DUtil_CreateFrame(m_graphroot)
  474.     
  475.     
  476.     
  477.     
  478.     Dim rc As RECT
  479.     Dim surf As Direct3DSurface8
  480.     Dim rts As D3DXRenderToSurface
  481.     Dim rtsviewport As D3DVIEWPORT8
  482.     
  483.     Call g_dev.GetDisplayMode(d3ddm)
  484.     Set rts = g_d3dx.CreateRenderToSurface(g_dev, kdx, kdy, d3ddm.format, 1, D3DFMT_D16)
  485.     rtsviewport.height = kdx
  486.     rtsviewport.width = kdy
  487.     rtsviewport.MaxZ = 1
  488.     
  489.     Set surf = m_Tex.GetSurfaceLevel(0)
  490.           
  491.     rts.BeginScene surf, rtsviewport
  492.     g_dev.Clear 0, ByVal 0, D3DCLEAR_TARGET Or D3DCLEAR_ZBUFFER, &HFFC0C0C0, 1, 0
  493.  
  494.  
  495.     
  496.     g_d3dx.DrawText m_font2, &HFF000000, "XXX", rc, DT_CALCRECT
  497.     m_font2height = rc.bottom
  498.     
  499.     
  500.     i = 0
  501.     Dim item As Variant
  502.     For Each item In m_RowLabels
  503.         If m_font2height * i >= kdy Then Exit For
  504.         rc.Top = m_font2height * i: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  505.         g_d3dx.DrawText m_font2, &HFF000000, item, rc, DT_CALCRECT
  506.         g_d3dx.DrawText m_font2, &HFF000000, item, rc, 0
  507.         i = i + 1
  508.     Next
  509.     For Each item In m_ColLabels
  510.         If m_font2height * i >= kdy Then Exit For
  511.         rc.Top = m_font2height * i: rc.Left = 10: rc.bottom = 0: rc.Right = 0
  512.         g_d3dx.DrawText m_font2, &HFF000000, item, rc, DT_CALCRECT
  513.         g_d3dx.DrawText m_font2, &HFF000000, item, rc, 0
  514.         i = i + 1
  515.     Next
  516.     
  517.     
  518.     rts.EndScene
  519.     
  520.     i = 0
  521.     Dim quadframe As CD3DFrame
  522.     
  523.     ReDim m_barmesh(0)
  524.     For Each entry In m_data
  525.         If entry.y >= 0 Then Set quadframe = m_quad1
  526.         If entry.y < 0 Then Set quadframe = m_quad2
  527.                 
  528.         'Set material of objects
  529.         material.diffuse = LONGtoD3DCOLORVALUE(entry.color)
  530.         material.Ambient = material.diffuse
  531.                 
  532.         'Create individual objects
  533.         Set newFrame = D3DUtil_CreateFrame(quadframe)
  534.         newFrame.SetScale 1
  535.         newFrame.SetPosition vec3(entry.x, entry.y / 2, entry.z)
  536.         
  537.         ReDim Preserve m_barmesh(i)
  538.         Set m_barmesh(i) = g_d3dx.CreateBox(g_dev, m_sizex, Abs(entry.y), m_sizez, Nothing)
  539.         newFrame.AddD3DXMesh(m_barmesh(i)).SetMaterialOverride material
  540.         
  541.         
  542.         
  543.         i = i + 1
  544.         newFrame.ObjectName = Str(i)
  545.     Next
  546.         
  547.     
  548.     Dim strLabel As Variant
  549.         
  550.     w = m_sizex * 3:  h = 0.5
  551.     
  552.     i = 0
  553.     If Not (m_cols = 0 Or m_rows = 0) Then
  554.    
  555.         ReDim m_labelmesh(m_rows + m_cols)
  556.         ReDim m_LabelTex(m_rows + m_cols)
  557.         
  558.         For Each strLabel In m_ColLabels
  559.         
  560.         i = i + 1
  561.         
  562.         su = 0: eu = 0.5:
  563.         sv = (m_font2height * (i - 1) / kdy)
  564.         ev = (m_font2height * (i) / kdy)
  565.                                     
  566.         Set newFrame = CreateSheetWithTextureCoords(w, h, su, eu, sv, ev, m_Tex)
  567.         newFrame.ObjectName = strLabel
  568.         newFrame.SetPosition vec3(5.5, -h / 2, (i - m_maxZ / 2 - 1) * m_scalez)
  569.         newFrame.AddRotation COMBINE_BEFORE, 0, 1, 0, g_pi / 2
  570.         m_graphroot.AddChild newFrame
  571.         
  572.         Set newFrame = CreateSheetWithTextureCoords(w, h, su, eu, sv, ev, m_Tex)
  573.         newFrame.ObjectName = strLabel
  574.         newFrame.SetPosition vec3(-5.5, 5 - h / 2, (i - m_maxZ / 2 - 1) * m_scalez)
  575.         newFrame.AddRotation COMBINE_BEFORE, 0, 1, 0, g_pi / 2
  576.         m_graphroot.AddChild newFrame
  577.         
  578.         su = 0: eu = 1: sv = 0: ev = 1
  579.         
  580.         LoadTexture i, m_RowTextures(i)    'note row and col texture are swapped
  581.         
  582.         If Not m_LabelTex(i) Is Nothing Then
  583.                 Set newFrame = CreateSheetWithTextureCoords(w, w, su, eu, sv, ev, m_LabelTex(i))
  584.                 newFrame.ObjectName = strLabel + " picture"
  585.                 newFrame.SetPosition vec3(5.5, -h - w / 2, (i - m_maxZ / 2 - 1) * m_scalez)
  586.                 newFrame.AddRotation COMBINE_BEFORE, 0, 1, 0, g_pi / 2
  587.                 m_graphroot.AddChild newFrame
  588.             End If
  589.              
  590.  
  591.         Next
  592.         
  593.         j = 0
  594.         For Each strLabel In m_RowLabels
  595.              Set newFrame = D3DUtil_CreateFrame(m_graphroot)
  596.              i = i + 1: j = j + 1
  597.              
  598.              
  599.              su = 0: eu = 0.5:
  600.              sv = (m_font2height * (i - 1) / kdy)
  601.              ev = (m_font2height * (i) / kdy)
  602.                                              
  603.              Set newFrame = CreateSheetWithTextureCoords(w, h, su, eu, sv, ev, m_Tex)
  604.              newFrame.ObjectName = strLabel
  605.              newFrame.SetPosition vec3((j - m_maxX / 2 - 1) * m_scalex, -h / 2, -5.5)
  606.              m_graphroot.AddChild newFrame
  607.              
  608.              Set newFrame = CreateSheetWithTextureCoords(w, h, su, eu, sv, ev, m_Tex)
  609.              newFrame.ObjectName = strLabel
  610.              newFrame.SetPosition vec3((j - m_maxX / 2 - 1) * m_scalex, 5 - h / 2, 5.5)
  611.              m_graphroot.AddChild newFrame
  612.              
  613.              su = 0: eu = 1: sv = 0: ev = 1
  614.              
  615.              LoadTexture i, m_ColTextures(j)    'note row and col texture are swapped
  616.              
  617.              If Not m_LabelTex(i) Is Nothing Then
  618.                 Set newFrame = CreateSheetWithTextureCoords(w, w, su, eu, sv, ev, m_LabelTex(i))
  619.                 newFrame.ObjectName = strLabel + " picture"
  620.                 newFrame.SetPosition vec3((j - m_maxX / 2 - 1) * m_scalex, -h - w / 2, -5.5)
  621.                 m_graphroot.AddChild newFrame
  622.             End If
  623.  
  624.         Next
  625.  
  626.     End If
  627.     
  628.         
  629.  
  630.  
  631.     
  632.     
  633.  
  634.    
  635.     m_bGraphInit = True
  636. End Sub
  637.  
  638.  
  639. Public Sub InitDeviceObjects()
  640.     
  641.     Dim d3ddm As D3DDISPLAYMODE
  642.     
  643.     If m_binit = False Then Exit Sub
  644.     
  645.  
  646.     Dim rc As RECT
  647.     
  648.     Set m_meshobj = g_d3dx.CreateBox(g_dev, 0.1, 0.1, 0.1, Nothing)
  649.     Set m_meshplane = g_d3dx.CreateBox(g_dev, 10, 10, 0.1, Nothing)
  650.     
  651.     Call g_dev.GetDisplayMode(d3ddm)
  652.     Set m_Tex = g_d3dx.CreateTexture(g_dev, kdx, kdx, 0, 0, d3ddm.format, D3DPOOL_MANAGED)
  653.     
  654.     Set m_font = g_d3dx.CreateFont(g_dev, m_vbfont.hFont)
  655.     Set m_font2 = g_d3dx.CreateFont(g_dev, m_vbfont2.hFont)
  656.        
  657.        
  658.     
  659. End Sub
  660.  
  661.  
  662.  
  663.  
  664. Private Sub DrawLines(quad As Long)
  665.     
  666.     g_dev.SetTransform D3DTS_WORLD, m_graphroot.GetMatrix
  667.     
  668.     DrawLine vec3(-5, 0.1, 0), vec3(5, 0.1, 0), &HFF0&
  669.     DrawLine vec3(0, 0.1, -5), vec3(0, 0.1, 5), &HFF0&
  670.     
  671. End Sub
  672.  
  673. Private Sub DrawLine(v1 As D3DVECTOR, v2 As D3DVECTOR, color As Long)
  674.     
  675.     Dim mat As D3DMATERIAL8
  676.     mat.diffuse = LONGtoD3DCOLORVALUE(color)
  677.     mat.Ambient = mat.diffuse
  678.     g_dev.SetMaterial mat
  679.     
  680.     Dim dataOut(2) As D3DVERTEX
  681.     LSet dataOut(0) = v1
  682.     LSet dataOut(1) = v2
  683.     g_dev.SetVertexShader D3DFVF_VERTEX
  684.     g_dev.DrawPrimitiveUP D3DPT_LINELIST, 1, dataOut(0), Len(dataOut(0))
  685.     
  686. End Sub
  687.  
  688.  
  689.  
  690. Public Sub MouseOver(Button As Integer, Shift As Integer, x As Single, y As Single)
  691.     
  692.     If m_binit = False Then Exit Sub
  693.         
  694.     
  695.     Dim pick As New CD3DPick
  696.     Dim frame As CD3DFrame
  697.     Dim nid As Long
  698.     Dim entry As DataEntry
  699.     
  700.     'remove the XZ plane from consideration for pick
  701.     m_XZPlaneFrame.Enabled = False
  702.     m_quad1.Enabled = True
  703.     m_quad2.Enabled = True
  704.     
  705.     
  706.     pick.ViewportPick m_graphroot, x, y
  707.     nid = pick.FindNearest()
  708.     If nid < 0 Then
  709.         m_drawtextEnable = False
  710.         Exit Sub
  711.     End If
  712.         
  713.     Set frame = pick.GetFrame(nid)
  714.     
  715.     'have matrices pre computed for scene graph
  716.     m_graphroot.UpdateFrames
  717.     
  718.     'due some math to get position of item in screen space
  719.     Dim viewport As D3DVIEWPORT8
  720.     Dim projmatrix As D3DMATRIX
  721.     Dim viewmatrix As D3DMATRIX
  722.     Dim vOut As D3DVECTOR
  723.     
  724.     g_dev.GetViewport viewport
  725.     g_dev.GetTransform D3DTS_PROJECTION, projmatrix
  726.     g_dev.GetTransform D3DTS_VIEW, viewmatrix
  727.     D3DXVec3Project vOut, vec3(0, 0, 0), viewport, projmatrix, viewmatrix, frame.GetUpdatedMatrix
  728.             
  729.     Debug.Print vOut.x, vOut.y, frame.ObjectName
  730.     
  731.     
  732.     Dim destRect As RECT
  733.     m_drawtextpos.Left = x - 20
  734.     m_drawtextpos.Top = y - 70
  735.     
  736.     If m_drawtextpos.Left < 0 Then m_drawtextpos.Left = 1
  737.     If m_drawtextpos.Top < 0 Then m_drawtextpos.Top = 1
  738.     
  739.     
  740.     Dim iOver As Long
  741.     If IsNumeric(frame.ObjectName) Then
  742.         iOver = val(frame.ObjectName)
  743.         Set entry = m_data.item(iOver)
  744.         With entry
  745.             m_drawtext = .dataname + Chr(13)
  746.         End With
  747.         m_drawtextEnable = True
  748.     End If
  749.  
  750. End Sub
  751.  
  752. Sub FrameMove()
  753.  
  754.     'for camera movement
  755.     m_fElapsedTime = DXUtil_Timer(TIMER_GETELLAPSEDTIME) * 1.3
  756.     If m_fElapsedTime < 0 Then Exit Sub
  757.         
  758.         
  759.     If m_bRot And m_bMouseDown = False Then
  760.         m_graphroot.AddRotation COMBINE_BEFORE, 0, 1, 0, (g_pi / 40) * m_fElapsedTime
  761.     End If
  762.         
  763.         
  764.     ' Slow things down for the REF device
  765.     If (g_devType = D3DDEVTYPE_REF) Then m_fElapsedTime = 0.05
  766.  
  767.     Dim fSpeed As Single
  768.     Dim fAngularSpeed
  769.     
  770.     fSpeed = 5 * m_fElapsedTime
  771.     fAngularSpeed = 1 * m_fElapsedTime
  772.  
  773.     ' Slowdown the camera movement
  774.     D3DXVec3Scale m_vVelocity, m_vVelocity, 0.9
  775.     m_fYawVelocity = m_fYawVelocity * 0.9
  776.     m_fPitchVelocity = m_fPitchVelocity * 0.9
  777.  
  778.     ' Process keyboard input
  779.     If (m_bKey(vbKeyRight)) Then m_vVelocity.x = m_vVelocity.x + fSpeed        '  Slide Right
  780.     If (m_bKey(vbKeyLeft)) Then m_vVelocity.x = m_vVelocity.x - fSpeed         '  Slide Left
  781.     If (m_bKey(vbKeyUp)) Then m_vVelocity.y = m_vVelocity.y + fSpeed           '  Move up
  782.     If (m_bKey(vbKeyDown)) Then m_vVelocity.y = m_vVelocity.y - fSpeed         '  Move down
  783.     
  784.     If (m_bKey(vbKeyW)) Then m_vVelocity.z = m_vVelocity.z + fSpeed            '  Move Forward
  785.     If (m_bKey(vbKeyS)) Then m_vVelocity.z = m_vVelocity.z - fSpeed            '  Move Backward
  786.     
  787.     If (m_bKey(vbKeyE)) Then m_fYawVelocity = m_fYawVelocity + fSpeed          '  Yaw right
  788.     If (m_bKey(vbKeyQ)) Then m_fYawVelocity = m_fYawVelocity - fSpeed          '  Yaw left
  789.     
  790.     If (m_bKey(vbKeyZ)) Then m_fPitchVelocity = m_fPitchVelocity + fSpeed      '  turn down
  791.     If (m_bKey(vbKeyA)) Then m_fPitchVelocity = m_fPitchVelocity - fSpeed      '  turn up
  792.     
  793.     
  794.  
  795.     ' Update the position vector
  796.     Dim vT As D3DVECTOR, vTemp As D3DVECTOR
  797.     D3DXVec3Scale vTemp, m_vVelocity, fSpeed
  798.     D3DXVec3Add vT, vT, vTemp
  799.     D3DXVec3TransformNormal vT, vT, m_matOrientation
  800.     D3DXVec3Add m_vPosition, m_vPosition, vT
  801.     
  802.     If (m_vPosition.y < 1) Then m_vPosition.y = 1
  803.  
  804.     ' Update the yaw-pitch-rotation vector
  805.     m_fYaw = m_fYaw + fAngularSpeed * m_fYawVelocity
  806.     m_fPitch = m_fPitch + fAngularSpeed * m_fPitchVelocity
  807.     If (m_fPitch < 0) Then m_fPitch = 0
  808.     If (m_fPitch > g_pi / 2) Then m_fPitch = g_pi / 2
  809.  
  810.     Dim qR As D3DQUATERNION, det As Single
  811.     D3DXQuaternionRotationYawPitchRoll qR, m_fYaw, m_fPitch, 0
  812.     D3DXMatrixAffineTransformation m_matOrientation, 1.25, vec3(0, 0, 0), qR, m_vPosition
  813.     D3DXMatrixInverse m_matView, det, m_matOrientation
  814.     
  815.         'set new view matrix
  816.     g_dev.SetTransform D3DTS_VIEW, m_matView
  817.  
  818. End Sub
  819.  
  820. Private Sub DirectXEvent8_DXCallback(ByVal i As Long)
  821.     Dim w As Single
  822.     Dim h As Single
  823.     
  824.     Dim w1 As Single, w2 As Single
  825.     Dim h1 As Single, h2 As Single
  826.     Dim sv As Single, ev As Single
  827.     Dim su As Single, eu As Single
  828.     
  829.     Dim mat As D3DMATERIAL8
  830.                   
  831.     w = m_sizex * 1.4:  h = 0.4
  832.         
  833.     mat.Ambient = ColorValue4(1, 1, 1, 1)
  834.     mat.diffuse = ColorValue4(1, 1, 1, 1)
  835.     
  836.         
  837.     sv = (m_font2height * (i) / kdy)
  838.     ev = (m_font2height * (i + 1) / kdy)
  839.     
  840.     'g_dev.SetTexture 0, m_Tex
  841.     'g_dev.SetMaterial mat
  842.     DrawLine vec3(1, 1, 1), vec3(0, 0, 0), &HFF00FF00
  843.     
  844.     w = m_sizex * 1.4:  h = 0.4
  845.     
  846.     'DrawSheet -w, w, -2 * h, 0, 0, 0.5, sv, ev
  847.     
  848.     'g_dev.SetTexture 0, m_LabelTex(i + 1)
  849.     'DrawSheet -w, w, -2 * h - 2 * w, -2 * h, 0, 1, 0, 1
  850.     
  851.     
  852. End Sub
  853.  
  854. Function CreateSheetWithTextureCoords(width As Single, height As Single, su As Single, eu As Single, sv As Single, ev As Single, texture As Direct3DTexture8) As CD3DFrame
  855.     Dim frame As CD3DFrame
  856.     Dim mesh As CD3DMesh
  857.     Dim retd3dxMesh As D3DXMesh
  858.     Dim vertexbuffer As Direct3DVertexBuffer8
  859.     Dim verts(8) As D3DVERTEX
  860.     Dim indices(12) As Integer
  861.     Dim w As Single, d As Single, h1 As Single, h2 As Single
  862.     
  863.     w = width / 2
  864.     h2 = height / 2
  865.     h1 = -height / 2
  866.     d = 0.01
  867.     
  868.     Dim whitematerial As D3DMATERIAL8
  869.     whitematerial.diffuse = ColorValue4(1, 1, 1, 1)
  870.     whitematerial.Ambient = whitematerial.diffuse
  871.         
  872.     'Create an empty d3dxmesh with room for 12 vertices and 12
  873.     Set retd3dxMesh = g_d3dx.CreateMeshFVF(8, 12, D3DXMESH_MANAGED, D3DFVF_VERTEX, g_dev)
  874.     
  875.     
  876.     'front face
  877.     
  878.     'add vertices
  879.     With verts(0): .x = -w: .y = h2: .z = -d: .nz = 1: .tu = su: .tv = sv: End With
  880.     With verts(1): .x = w: .y = h2: .z = -d: .nz = 1: .tu = eu: .tv = sv: End With
  881.     With verts(2): .x = w: .y = h1: .z = -d: .nz = 1: .tu = eu: .tv = ev: End With
  882.     With verts(3): .x = -w: .y = h1: .z = -d: .nz = 1: .tu = su: .tv = ev: End With
  883.     
  884.     'connect verices to make 2 triangles per face
  885.     indices(0) = 0: indices(1) = 1: indices(2) = 2
  886.     indices(3) = 0: indices(4) = 2: indices(5) = 3
  887.     
  888.     'back face
  889.     With verts(4): .x = -w: .y = h1: .z = d: .nz = -1: .tu = eu: .tv = ev: End With
  890.     With verts(5): .x = w: .y = h1: .z = d: .nz = -1: .tu = su: .tv = ev: End With
  891.     With verts(6): .x = w: .y = h2: .z = d: .nz = -1: .tu = su: .tv = sv: End With
  892.     With verts(7): .x = -w: .y = h2: .z = d: .nz = -1: .tu = eu: .tv = sv: End With
  893.     indices(6) = 4: indices(7) = 5: indices(8) = 6
  894.     indices(9) = 4: indices(10) = 6: indices(11) = 7
  895.     
  896.         
  897.     
  898.     D3DXMeshVertexBuffer8SetData retd3dxMesh, 0, Len(verts(0)) * 8, 0, verts(0)
  899.     D3DXMeshIndexBuffer8SetData retd3dxMesh, 0, Len(indices(0)) * 12, 0, indices(0)
  900.         
  901.     Set frame = New CD3DFrame
  902.     Set mesh = frame.AddD3DXMesh(retd3dxMesh)
  903.     
  904.     mesh.bUseMaterials = True
  905.     mesh.SetMaterialCount 1
  906.     mesh.SetMaterial 0, whitematerial
  907.     mesh.SetMaterialTexture 0, texture
  908.     
  909.     Set CreateSheetWithTextureCoords = frame
  910. End Function
  911.  
  912. Sub DrawSheet(w1 As Single, w2 As Single, h1 As Single, h2 As Single, su As Single, eu As Single, sv As Single, ev As Single)
  913.     Dim verts(4) As D3DVERTEX
  914.  
  915.     g_dev.SetTexture 0, Nothing
  916.     
  917.     With verts(0): .x = w1: .y = h1: .tu = su: .tv = ev: .nz = -1: End With
  918.     With verts(1): .x = w2: .y = h1: .tu = eu: .tv = ev: .nz = -1: End With
  919.     With verts(2): .x = w2: .y = h2: .tu = eu: .tv = sv: .nz = -1: End With
  920.     With verts(3): .x = w1: .y = h2: .tu = su: .tv = sv: .nz = -1: End With
  921.     'g_dev.SetVertexShader D3DFVF_VERTEX
  922.     'g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  923.     
  924.     
  925.     With verts(0): .z = 0.01: .x = w2: .y = h1: .tu = su: .tv = ev: .nz = 1: End With
  926.     With verts(1): .z = 0.01: .x = w1: .y = h1: .tu = eu: .tv = ev: .nz = 1: End With
  927.     With verts(2): .z = 0.01: .x = w1: .y = h2: .tu = eu: .tv = sv: .nz = 1: End With
  928.     With verts(3): .z = 0.01: .x = w2: .y = h2: .tu = su: .tv = sv: .nz = 1: End With
  929.     'g_dev.SetVertexShader D3DFVF_VERTEX
  930.     'g_dev.DrawPrimitiveUP D3DPT_TRIANGLEFAN, 2, verts(0), Len(verts(0))
  931.  
  932. End Sub
  933.  
  934. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  935.     m_bKey(KeyCode) = True
  936. End Sub
  937.  
  938. Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
  939.     m_bKey(KeyCode) = False
  940. End Sub
  941.  
  942.  
  943. Private Sub Form_Load()
  944.  
  945.  
  946.     Me.Show
  947.     DoEvents
  948.     
  949.  
  950.     m_mediadir = FindMediaDir("bargraphdata.csv")
  951.     D3DUtil_SetMediaPath m_mediadir
  952.     
  953.     Init Me.hwnd, Me.font, Command1.font
  954.     
  955.     'Start the timers and callbacks
  956.     Call DXUtil_Timer(TIMER_start)
  957.     Timer1.Enabled = True
  958.  
  959.     
  960.  
  961. End Sub
  962.  
  963. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  964.     If Button = 2 Then
  965.         Me.PopupMenu MENU_POPUP
  966.     Else
  967.     
  968.         '- save our current position
  969.         m_bMouseDown = True
  970.         m_lastX = x
  971.         m_lasty = y
  972.         
  973.     End If
  974. End Sub
  975.  
  976. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  977.         
  978.     If m_binit = False Then Exit Sub
  979.     
  980.     If Button = 2 Then Exit Sub
  981.     If m_bMouseDown = False Then
  982.         Call MouseOver(Button, Shift, x, y)
  983.     Else
  984.         '- Rotate the object
  985.         RotateTrackBall CInt(x), CInt(y)
  986.     End If
  987.     
  988.     FrameMove
  989.     DrawGraph
  990.     
  991. End Sub
  992.  
  993. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  994.     m_bMouseDown = False
  995. End Sub
  996.  
  997. '-----------------------------------------------------------------------------
  998. ' Name: Form_Resize()
  999. ' Desc: hadle resizing of the D3D backbuffer
  1000. '-----------------------------------------------------------------------------
  1001. Private Sub Form_Resize()
  1002.     
  1003.     
  1004.     Timer1.Enabled = False
  1005.     
  1006.     ' If D3D is not initialized then exit
  1007.     If Not m_binit Then Exit Sub
  1008.     
  1009.     ' If we are in a minimized state stop the timer and exit
  1010.     If Me.WindowState = vbMinimized Then
  1011.         DXUtil_Timer TIMER_STOP
  1012.         m_bMinimized = True
  1013.         Exit Sub
  1014.         
  1015.     ' If we just went from a minimized state to maximized
  1016.     ' restart the timer
  1017.     Else
  1018.         If m_bMinimized = True Then
  1019.             DXUtil_Timer TIMER_start
  1020.             m_bMinimized = False
  1021.         End If
  1022.     End If
  1023.         
  1024.      ' Dont let the window get too small
  1025.     If Me.ScaleWidth < 10 Then
  1026.         Me.width = Screen.TwipsPerPixelX * 10
  1027.         Exit Sub
  1028.     End If
  1029.     
  1030.     If Me.ScaleHeight < 10 Then
  1031.         Me.height = Screen.TwipsPerPixelY * 10
  1032.         Exit Sub
  1033.     End If
  1034.     
  1035.     'remove references to FONTs
  1036.     DeleteDeviceObjects
  1037.     
  1038.     'reset and resize our D3D backbuffer to the size of the window
  1039.     D3DUtil_ResizeWindowed Me.hwnd
  1040.     
  1041.     'All state get losts after a reset so we need to reinitialze it here
  1042.     RestoreDeviceObjects
  1043.     
  1044.     Timer1.Enabled = True
  1045.     
  1046. End Sub
  1047.  
  1048. '- Rotate Track ball
  1049. '  given a point on the screen the mouse was moved to
  1050. '  simulate a track ball
  1051. Private Sub RotateTrackBall(x As Integer, y As Integer)
  1052.  
  1053.     
  1054.     Dim delta_x As Single, delta_y As Single
  1055.     Dim delta_r As Single, radius As Single, denom As Single, angle As Single
  1056.     
  1057.     ' rotation axis in camcoords, worldcoords, sframecoords
  1058.     Dim axisC As D3DVECTOR
  1059.     Dim wc As D3DVECTOR
  1060.     Dim axisS As D3DVECTOR
  1061.     Dim base As D3DVECTOR
  1062.     Dim origin As D3DVECTOR
  1063.     
  1064.     delta_x = x - m_lastX
  1065.     delta_y = y - m_lasty
  1066.     m_lastX = x
  1067.     m_lasty = y
  1068.  
  1069.             
  1070.      delta_r = Sqr(delta_x * delta_x + delta_y * delta_y)
  1071.      radius = 50
  1072.      denom = Sqr(radius * radius + delta_r * delta_r)
  1073.     
  1074.     If (delta_r = 0 Or denom = 0) Then Exit Sub
  1075.     angle = (delta_r / denom)
  1076.  
  1077.     axisC.x = (-delta_y / delta_r)
  1078.     axisC.y = (-delta_x / delta_r)
  1079.     axisC.z = 0
  1080.  
  1081.  
  1082.     'transform camera space vector to world space
  1083.     'm_largewindow.m_cameraFrame.Transform wc, axisC
  1084.     g_dev.GetTransform D3DTS_VIEW, g_viewMatrix
  1085.     D3DXVec3TransformCoord wc, axisC, g_viewMatrix
  1086.     
  1087.     
  1088.     'transform world space vector into Model space
  1089.     m_graphroot.UpdateFrames
  1090.     axisS = m_graphroot.InverseTransformCoord(wc)
  1091.         
  1092.     'transform origen camera space to world coordinates
  1093.     'm_largewindow.m_cameraFrame.Transform  wc, origin
  1094.     D3DXVec3TransformCoord wc, origin, g_viewMatrix
  1095.     
  1096.     'transfer cam space origen to model space
  1097.     base = m_graphroot.InverseTransformCoord(wc)
  1098.     
  1099.     axisS.x = axisS.x - base.x
  1100.     axisS.y = axisS.y - base.y
  1101.     axisS.z = axisS.z - base.z
  1102.     
  1103.     m_graphroot.AddRotation COMBINE_BEFORE, axisS.x, axisS.y, axisS.z, angle
  1104.     
  1105. End Sub
  1106.  
  1107.  
  1108. Private Sub Form_Paint()
  1109.     If Not m_binit Then Exit Sub
  1110.     If Not m_bGraphInit Then Exit Sub
  1111.     DrawGraph
  1112. End Sub
  1113.  
  1114. Private Sub Form_Unload(Cancel As Integer)
  1115.     End
  1116. End Sub
  1117.  
  1118. Private Sub MENU_BASE_Click()
  1119.     m_bShowBase = Not m_bShowBase
  1120.     MENU_BASE.Checked = m_bShowBase
  1121. End Sub
  1122.  
  1123.  
  1124. Private Sub MENU_LOAD_Click()
  1125.     Dim sFile As String
  1126.     
  1127.     
  1128.     'Stop the timers and callbacks
  1129.     Timer1.Enabled = False
  1130.     
  1131.     
  1132.     
  1133.     CommonDialog1.FileName = ""
  1134.     CommonDialog1.DefaultExt = "csv"
  1135.     CommonDialog1.filter = "csv|*.csv"
  1136.     CommonDialog1.InitDir = m_mediadir
  1137.     
  1138.     
  1139.     'On Local Error Resume Next
  1140.     CommonDialog1.ShowOpen
  1141.     sFile = CommonDialog1.FileName
  1142.     If sFile = "" Then Exit Sub
  1143.     LoadFileAsBarGraph sFile
  1144.     
  1145.     
  1146.     D3DUtil_Destory
  1147.     DestroyDeviceObjects
  1148.             
  1149.     D3DUtil_Init Me.hwnd, True, 0, 0, D3DDEVTYPE_HAL, Nothing
  1150.     InitDeviceObjects
  1151.     ComputeDataExtents
  1152.     BuildGraph
  1153.     RestoreDeviceObjects
  1154.     
  1155.     'restart the callbacks
  1156.     DXUtil_Timer (TIMER_RESET)
  1157.     DXUtil_Timer (TIMER_start)
  1158.     Timer1.Enabled = True
  1159. End Sub
  1160.  
  1161. Private Sub MENU_RESET_Click()
  1162.     m_graphroot.SetMatrix g_identityMatrix
  1163.     m_vPosition = vec3(0, 0, -20)
  1164.     m_fYaw = 0
  1165.     m_fPitch = 0
  1166.  
  1167.     Call D3DXMatrixTranslation(m_matOrientation, 0, 0, 0)
  1168.     D3DUtil_SetupDefaultScene
  1169.     g_dev.GetTransform D3DTS_VIEW, m_matView
  1170. End Sub
  1171.  
  1172. Private Sub MENU_ROTATE_Click()
  1173.     m_bRot = Not m_bRot
  1174.     MENU_ROTATE.Checked = m_bRot
  1175. End Sub
  1176.  
  1177. Private Sub Timer1_Timer()
  1178.     If Not m_binit Then Exit Sub
  1179.     
  1180.     FrameMove
  1181.     DrawGraph
  1182. End Sub
  1183.  
  1184. Sub LoadFileAsBarGraph(sFile As String)
  1185.     
  1186.     
  1187.     If Dir$(sFile) = "" Then
  1188.         MsgBox "Unable to find " & sFile
  1189.         Exit Sub
  1190.     End If
  1191.     
  1192.     Dim fl As Long
  1193.     Dim strIn As String
  1194.     Dim strTrim As String
  1195.     Dim strFirstChar As String
  1196.     Dim splitArray
  1197.     Dim cols As Long
  1198.     Dim bFoundData As Boolean
  1199.     Dim bFoundHeader As Boolean
  1200.     Dim sName As String
  1201.     Dim x As Double
  1202.     Dim y As Double
  1203.     Dim z As Double
  1204.     Dim i As Long
  1205.     Dim olddata As Collection
  1206.     Dim oldcolLabels As Collection
  1207.     Dim oldRowLabels As Collection
  1208.     Dim oldCols As Long
  1209.     Dim oldRows As Long
  1210.     Dim strRowLabel As String
  1211.     Dim strColLabel As String
  1212.     Dim valout As Variant
  1213.     Dim strName As String
  1214.     Dim sizeout As Single
  1215.     Dim colorout As Long
  1216.     
  1217.     fl = FreeFile
  1218.         
  1219.     'On Local Error GoTo errOut
  1220.     
  1221.     Set olddata = m_data
  1222.     Set oldcolLabels = m_ColLabels
  1223.     Set oldRowLabels = m_RowLabels
  1224.     oldCols = m_cols
  1225.     oldRows = m_rows
  1226.     
  1227.     Set m_data = Nothing
  1228.     Set m_data = New Collection
  1229.     
  1230.     m_cols = 0
  1231.     m_rows = 0
  1232.     
  1233.     Set m_ColLabels = New Collection
  1234.     Set m_RowLabels = New Collection
  1235.     
  1236.     Open sFile For Input As fl
  1237.         
  1238.     Do While Not EOF(fl)
  1239.         Line Input #fl, strIn
  1240.         strTrim = Trim(strIn)
  1241.         
  1242.         'skip comment lines
  1243.         strFirstChar = Mid$(strTrim, 1, 1)
  1244.         If strFirstChar = "#" Or strFirstChar = ";" Then GoTo nextLine
  1245.         If strTrim = "" Then GoTo nextLine
  1246.         
  1247.         splitArray = Split(strTrim, ",")
  1248.         
  1249.         cols = UBound(splitArray) + 1
  1250.         If cols < 2 Then
  1251.             MsgBox "Comma delimited file must have at least a header row, header column, and data"
  1252.             GoTo closeOut
  1253.         End If
  1254.                 
  1255.         Dim strData As String
  1256.         Dim q As Long
  1257.         
  1258.         'If we have not found numbers see if we found a header row
  1259.         If Not bFoundData Then
  1260.             If IsNumeric(splitArray(1)) = False Then
  1261.                 
  1262.                 'assume data is a header row
  1263.                 m_cols = cols
  1264.                 
  1265.                 m_GraphTitle = CStr(splitArray(0))
  1266.                                 
  1267.                 ReDim m_ColTextures(UBound(splitArray))
  1268.                 
  1269.                 For i = 1 To m_cols - 1
  1270.                     strData = Trim(CStr(splitArray(i)))
  1271.                     strColLabel = strData
  1272.                     q = InStr(UCase(strData), "TEXTURE:")
  1273.                     If q <> 0 Then
  1274.                         m_ColTextures(i) = Mid$(strData, q + 8)
  1275.                         If q > 1 Then strColLabel = Mid$(strData, 1, q - 1)
  1276.                     End If
  1277.                     m_ColLabels.Add strColLabel
  1278.                 Next
  1279.                 bFoundHeader = True
  1280.                 GoTo nextLine
  1281.             Else
  1282.                 bFoundData = True
  1283.                 If bFoundHeader = False Then
  1284.                     MsgBox "Comma delimited file must have first for be header row to label columns"
  1285.                     GoTo closeOut
  1286.                 End If
  1287.             End If
  1288.         End If
  1289.         
  1290.         m_rows = m_rows + 1
  1291.         strData = Trim(splitArray(0))
  1292.         strRowLabel = strData
  1293.         q = InStr(UCase(strData), "TEXTURE:")
  1294.         ReDim Preserve m_RowTextures(m_rows)
  1295.         If q <> 0 Then
  1296.             m_RowTextures(m_rows) = Mid$(strData, q + 8)
  1297.             If q > 1 Then strRowLabel = Mid$(strData, 1, q - 1)
  1298.         End If
  1299.         
  1300.         m_RowLabels.Add strRowLabel
  1301.         
  1302.         sizeout = 1
  1303.         
  1304.         
  1305.         For i = 1 To m_cols - 1
  1306.             colorout = D3DCOLORVALUEtoLONG(ColorValue4(1, 1 - (2 + m_rows Mod 4) / 10, 0.2, 1 - ((i Mod 8)) / 10))
  1307.             strColLabel = m_ColLabels.item(i)
  1308.             valout = splitArray(i)
  1309.             strName = "(" & strRowLabel & "," & strColLabel & ") = " & CStr(valout)
  1310.             AddEntry strName, CDbl(i - 1), val(valout), CDbl(m_rows - 1), CDbl(sizeout), colorout, ""
  1311.         Next
  1312.         
  1313.         
  1314. nextLine:
  1315.     Loop
  1316.     
  1317.     Set olddata = Nothing
  1318.     Close fl
  1319.     
  1320.     m_sizex = (kScale / m_cols) * 0.5
  1321.     m_sizez = (kScale / m_rows) * 0.5
  1322.     
  1323.     Exit Sub
  1324.     
  1325. errOut:
  1326.     MsgBox "there was an error loading " & sFile
  1327.  
  1328. closeOut:
  1329.     
  1330.     'restore state
  1331.     Set m_data = olddata
  1332.     Set m_ColLabels = oldcolLabels
  1333.     Set m_RowLabels = oldRowLabels
  1334.     m_rows = oldRows
  1335.     m_cols = oldCols
  1336.     
  1337.     Close fl
  1338. End Sub
  1339.  
  1340. Function CreateBoxWithTextureCoords(width As Single, height As Single, depth As Single) As D3DXMesh
  1341.     Dim mesh As CD3DMesh
  1342.     Dim retd3dxMesh As D3DXMesh
  1343.     Dim vertexbuffer As Direct3DVertexBuffer8
  1344.     Dim verts(28) As D3DVERTEX
  1345.     Dim indices(36) As Integer
  1346.     Dim w As Single, d As Single, h1 As Single, h2 As Single
  1347.     w = width / 2
  1348.     h2 = height / 2
  1349.     h1 = -height / 2
  1350.     d = depth / 2
  1351.     
  1352.     'Create an empty d3dxmesh with room for 12 vertices and 12
  1353.     Set retd3dxMesh = g_d3dx.CreateMeshFVF(4 * 6, 6 * 6, D3DXMESH_MANAGED, D3DFVF_VERTEX, g_dev)
  1354.     
  1355.     
  1356.     'front face
  1357.     
  1358.     'add vertices
  1359.     With verts(0): .x = -w: .y = h2: .z = -d: .nz = 1: .tu = 0: .tv = 0: End With
  1360.     With verts(1): .x = w: .y = h2: .z = -d: .nz = 1: .tu = 1: .tv = 0: End With
  1361.     With verts(2): .x = w: .y = h1: .z = -d: .nz = 1: .tu = 1: .tv = 1: End With
  1362.     With verts(3): .x = -w: .y = h1: .z = -d: .nz = 1: .tu = 0: .tv = 1: End With
  1363.     
  1364.     'connect verices to make 2 triangles per face
  1365.     indices(0) = 0: indices(1) = 1: indices(2) = 2
  1366.     indices(3) = 0: indices(4) = 2: indices(5) = 3
  1367.     
  1368.     'back face
  1369.     With verts(4): .x = -w: .y = h1: .z = d: .nz = -1: .tu = 0: .tv = 1: End With
  1370.     With verts(5): .x = w: .y = h1: .z = d: .nz = -1: .tu = 1: .tv = 1: End With
  1371.     With verts(6): .x = w: .y = h2: .z = d: .nz = -1: .tu = 1: .tv = 0: End With
  1372.     With verts(7): .x = -w: .y = h2: .z = d: .nz = -1: .tu = 0: .tv = 0: End With
  1373.     indices(6) = 4: indices(7) = 5: indices(8) = 6
  1374.     indices(9) = 4: indices(10) = 6: indices(11) = 7
  1375.     
  1376.     'right face
  1377.     With verts(8): .x = w: .y = h1: .z = -d: .nx = -1: .tu = 0: .tv = 0: End With
  1378.     With verts(9): .x = w: .y = h1: .z = d: .nx = -1: .tu = 1: .tv = 0: End With
  1379.     With verts(10): .x = w: .y = h2: .z = d: .nx = -1: .tu = 1: .tv = 1: End With
  1380.     With verts(11): .x = w: .y = h2: .z = -d: .nx = -1: .tu = 0: .tv = 1: End With
  1381.     indices(12) = 8: indices(13) = 9: indices(14) = 10
  1382.     indices(15) = 8: indices(16) = 10: indices(17) = 11
  1383.     
  1384.     'left face
  1385.     With verts(16): .x = -w: .y = h2: .z = -d: .nx = 1: .tu = 0: .tv = 1: End With
  1386.     With verts(17): .x = -w: .y = h2: .z = d: .nx = 1: .tu = 1: .tv = 1: End With
  1387.     With verts(18): .x = -w: .y = h1: .z = d: .nx = 1: .tu = 1: .tv = 0: End With
  1388.     With verts(19): .x = -w: .y = h1: .z = -d: .nx = 1: .tu = 0: .tv = 0: End With
  1389.     indices(18) = 16: indices(19) = 17: indices(20) = 18
  1390.     indices(21) = 16: indices(22) = 18: indices(23) = 19
  1391.     
  1392.     'top face
  1393.     With verts(20): .x = -w: .y = h2: .z = -d: .ny = -1: .tu = 0: .tv = 0: End With
  1394.     With verts(21): .x = -w: .y = h2: .z = d: .ny = -1: .tu = 1: .tv = 0: End With
  1395.     With verts(22): .x = w: .y = h2: .z = d: .ny = -1: .tu = 1: .tv = 1: End With
  1396.     With verts(23): .x = w: .y = h2: .z = -d: .ny = -1: .tu = 0: .tv = 1: End With
  1397.     indices(24) = 20: indices(25) = 21: indices(26) = 22
  1398.     indices(27) = 20: indices(28) = 22: indices(29) = 23
  1399.         
  1400.     'bottom  face
  1401.     With verts(24): .x = w: .y = h1: .z = -d: .ny = 1: .tu = 0: .tv = 1: End With
  1402.     With verts(25): .x = w: .y = h1: .z = d: .ny = 1: .tu = 1: .tv = 1: End With
  1403.     With verts(26): .x = -w: .y = h1: .z = d: .ny = 1: .tu = 1: .tv = 0: End With
  1404.     With verts(27): .x = -w: .y = h1: .z = -d: .ny = 1: .tu = 0: .tv = 0: End With
  1405.     indices(30) = 24: indices(31) = 25: indices(32) = 26
  1406.     indices(33) = 24: indices(34) = 26: indices(35) = 27
  1407.         
  1408.     
  1409.     D3DXMeshVertexBuffer8SetData retd3dxMesh, 0, Len(verts(0)) * 28, 0, verts(0)
  1410.     D3DXMeshIndexBuffer8SetData retd3dxMesh, 0, Len(indices(0)) * 36, 0, indices(0)
  1411.         
  1412.         
  1413.     
  1414.     Set CreateBoxWithTextureCoords = retd3dxMesh
  1415. End Function
  1416.  
  1417. Sub LoadTexture(i As Long, strFile As String)
  1418.         
  1419.     If strFile = "" Then Exit Sub
  1420.     
  1421.     Set m_LabelTex(i) = D3DUtil.D3DUtil_CreateTextureInPool(g_dev, strFile, D3DFMT_R5G6B5)
  1422.     If m_LabelTex(i) Is Nothing Then
  1423.         MsgBox "Unable to find " & strFile
  1424.     End If
  1425. End Sub
  1426.